package com.mashibing.servicedriveruser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashibing.servicedriveruser.mapper.DriverUserWorkStatusMapper;
import org.mashibing.constant.DriverCarConstant;
import org.mashibing.dto.DriverUser;
import com.mashibing.servicedriveruser.mapper.DriverUserMapper;
import com.mashibing.servicedriveruser.service.DriverUserService;
import lombok.extern.slf4j.Slf4j;
import org.mashibing.dto.DriverUserWorkStatus;
import org.mashibing.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author:
 * @create: 2024/4/26 10:28
 * @description:
 */
@Service
@Slf4j
public class DriverUserServiceImpl extends ServiceImpl<DriverUserMapper, DriverUser> implements DriverUserService {

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Override
    public ResponseResult addUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now);
        driverUser.setGmtModified(now);
        this.baseMapper.insert(driverUser);
        // 初始化 司机工作状态表
        DriverUserWorkStatus userWorkStatus = new DriverUserWorkStatus();
        userWorkStatus.setDriverId(driverUser.getId());
        userWorkStatus.setWorkStatus(DriverCarConstant.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatusMapper.insert(userWorkStatus);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult updateUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtModified(now);
        this.baseMapper.updateById(driverUser);
        return ResponseResult.success();
    }

    @Override
    public DriverUser getDriverUserByPhone(String driverPhone) {
        QueryWrapper<DriverUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_phone",driverPhone);
        queryWrapper.eq("stats", DriverCarConstant.DRIVER_STATUS_VALID);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
